# supporting functions
import pygame
from csv import reader
from mazesettings import tile_size
from os import walk

# import the csv file established by Tiled
def import_csv_layout(path):
    terrain_map = [] # set this list to store the data imported from csv files
    with open(path) as map:
        level = reader(map,delimiter = ',') # use ',' as segmentation
        for row in level:
            terrain_map.append(list(row))
        return terrain_map # return the list

# import the graphics used in Tiled and cut them into corresponding part
def import_cut_graphic(path):
    surface = pygame.image.load(path).convert_alpha()
    tile_num_x = int(surface.get_size()[0] / tile_size) # x axis
    tile_num_y = int(surface.get_size()[1] / tile_size) # y axis

    cut_tiles = [] # set this list to store the fraction of a cut picture
    for row in range(tile_num_y):
        for col in range(tile_num_x):
            x = col * tile_size
            y = row * tile_size
            new_surface = pygame.Surface((tile_size,tile_size), flags = pygame.SRCALPHA)
            new_surface.blit(surface, (0,0), pygame.Rect(x,y,tile_size,tile_size))
            cut_tiles.append(new_surface)

    return cut_tiles

# import the player image file(can use animation)
def import_folder(path):
    surface_list = []

    for _,__,img_files in walk(path):#the path to the file
        for image in img_files:
            full_path = path + '/' + image
            image_surf =  pygame.image.load(full_path).convert_alpha()
            surface_list.append(image_surf)
    
    return surface_list